package com.nulldev.util.net.packets;

import java.util.LinkedList;
import com.nulldev.util.napi.nullAPI;
import com.nulldev.util.net.tcp.client.TCPClient.ClientConnection;
import com.nulldev.util.net.tcp.server.TCPServer.ServerConnection;

public class ConnectionManager {

	private static final LinkedList<Connectable> hooks = new LinkedList<Connectable>();
	public static boolean registerConnectable(final Connectable conn) {
		final boolean result = hooks.add(conn);
		return result;
	}
	
	public static nullAPI getServerConnection(final ServerConnection self) {
		final nullAPI na = new nullAPI();
		
		final LinkedList<nullAPI> fromConn = new LinkedList<nullAPI>();
		for (final Connectable conn : (LinkedList<Connectable>) hooks.clone()) {
			fromConn.add(conn.registerServer(self));
		}
		
		for (final nullAPI api : fromConn) {
			na.merge(api);
		}
		
		return na;
	}
	
	public static nullAPI getClientConnection(final ClientConnection self) {
		final nullAPI na = new nullAPI();
		
		final LinkedList<nullAPI> fromConn = new LinkedList<nullAPI>();
		for (final Connectable conn : (LinkedList<Connectable>) hooks.clone()) {
			fromConn.add(conn.registerClient(self));
		}
		
		for (final nullAPI api : fromConn) {
			na.merge(api);
		}
		
		return na;
	}

}
